package com.docshell.common;


import com.docshell.exceptions.DocShellException;
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;

public class SessionManager {

	private static IDfClientX goClientx;

	private static IDfSessionManager goSessionMgr = null;
    private static IDfSession idfSession = null;

    private static String m_UserName = null;
    private static String m_Password = null;
    private static String m_Docbase = null;
    
    private static boolean m_isConnected = false;

	public static IDfSession getSession() throws DocShellException {
		try {
			if(idfSession == null){
				idfSession = goSessionMgr.getSession(getDocbase());
				setIsConnected(true);
			}
		} catch (DfException dfe) {
			throw new DocShellException(DocShellException.DFC_EXCEPTION, dfe.toString());
		}
		return idfSession;
	}

	public static void releaseSession() {
		if (goSessionMgr!=null) {
			if (idfSession!=null) {
				goSessionMgr.release(idfSession);
				idfSession=null;
				setIsConnected(false);
			}
			goSessionMgr=null;
		}
	}

	public static void connectToDocbase(String a_UserName, String a_Password, String a_Docbase) throws DocShellException {
		
		try {
			if (idfSession==null) {
				goSessionMgr = createSessionManager();
		
				setUserName(a_UserName);
				setPassword(a_Password);
				setDocbase(a_Docbase);
				
				IDfLoginInfo loLoginInfo = goClientx.getLoginInfo();
				loLoginInfo.setUser(getUserName());
				loLoginInfo.setPassword(getPassword());
	        
	        	goSessionMgr.setIdentity(getDocbase(), loLoginInfo);
	        	idfSession = goSessionMgr.getSession(getDocbase());
	        	if (idfSession!=null) {
	        		setIsConnected(true);
	        	}
			}
		} catch (DfException dfe) {
			throw new DocShellException(DocShellException.DFC_EXCEPTION, dfe.toString());
		} catch (Exception e) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, e.toString());
		}
	}

	public static void connectToDocbase() throws DocShellException {
		
		try {
			if (idfSession==null) {
				goSessionMgr = createSessionManager();
		
				IDfLoginInfo loLoginInfo = goClientx.getLoginInfo();
				loLoginInfo.setUser(getUserName());
				loLoginInfo.setPassword(getPassword());
	        
	        	goSessionMgr.setIdentity(getDocbase(), loLoginInfo);
	        	idfSession = goSessionMgr.getSession(getDocbase());
	        	if (idfSession!=null) {
	        		setIsConnected(true);
	        	}
			}
		} catch (DfException dfe) {
			throw new DocShellException(DocShellException.DFC_EXCEPTION, dfe.toString());
		} catch (Exception e) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, e.toString());
		}
	}

	private static IDfSessionManager createSessionManager() throws Exception
    {
		goClientx = new DfClientX();
        IDfClient loClient = goClientx.getLocalClient();
        IDfSessionManager sessMgr = loClient.newSessionManager();
        return sessMgr;
    }

	public static void setUserName(String m_UserName) {
		SessionManager.m_UserName = m_UserName;
	}

	public static String getUserName() {
		return m_UserName;
	}

	public static void setPassword(String m_Password) {
		SessionManager.m_Password = m_Password;
	}

	public static String getPassword() {
		return m_Password;
	}

	public static void setDocbase(String m_Docbase) {
		SessionManager.m_Docbase = m_Docbase;
	}

	public static String getDocbase() {
		return m_Docbase;
	}

	public static void cleanUp() {
		// TODO Auto-generated method stub
		
	}

	public static void initialize() throws DocShellException {
		connectToDocbase();
	}

	public static void setIsConnected(boolean m_isConnected) {
		SessionManager.m_isConnected = m_isConnected;
	}

	public static boolean getIsConnected() {
		return m_isConnected;
	}	
}
